package com.newshub.app.ui.video

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.newshub.app.databinding.FragmentVideoBinding
import com.newshub.app.ui.video.VideoNewsAdapter
import com.newshub.app.ui.video.VideoPlayerActivity
import com.newshub.app.ui.video.VideoViewModel

class VideoFragment : Fragment() {
    
    private var _binding: FragmentVideoBinding? = null
    private val binding get() = _binding!!
    
    private lateinit var viewModel: VideoViewModel
    private lateinit var videoAdapter: VideoNewsAdapter
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentVideoBinding.inflate(inflater, container, false)
        return binding.root
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        initViewModel()
        initRecyclerView()
        setupSwipeRefresh()
        loadVideos()
    }
    
    private fun initViewModel() {
        viewModel = ViewModelProvider(this)[VideoViewModel::class.java]
        
        // 观察视频新闻列表
        viewModel.videoNews.observe(viewLifecycleOwner) { videoList ->
            videoAdapter.submitList(videoList)
            
            // 显示或隐藏空状态
            binding.emptyStateTextView.visibility = if (videoList.isEmpty()) View.VISIBLE else View.GONE
        }
        
        // 观察加载状态
        viewModel.isLoading.observe(viewLifecycleOwner) { isLoading ->
            binding.progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
            binding.swipeRefreshLayout.isRefreshing = false
        }
        
        // 观察错误信息
        viewModel.errorMessage.observe(viewLifecycleOwner) { errorMessage ->
            if (errorMessage != null) {
                Toast.makeText(requireContext(), errorMessage, Toast.LENGTH_SHORT).show()
            }
        }
    }
    
    private fun initRecyclerView() {
        videoAdapter = VideoNewsAdapter { video ->
            playVideo(video.url, video.title)
        }
        
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = videoAdapter
        }
    }
    
    private fun setupSwipeRefresh() {
        binding.swipeRefreshLayout.setOnRefreshListener {
            loadVideos()
        }
    }
    
    private fun loadVideos() {
        viewModel.loadVideoNews()
    }
    
    private fun playVideo(videoUrl: String, title: String) {
        val intent = Intent(requireContext(), VideoPlayerActivity::class.java)
        intent.putExtra(VideoPlayerActivity.EXTRA_VIDEO_URL, videoUrl)
        intent.putExtra(VideoPlayerActivity.EXTRA_VIDEO_TITLE, title)
        startActivity(intent)
    }
    
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}